package com.czy.owner.ui.archive;

import android.content.Intent;
import android.graphics.Paint;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.api.MaintenanceDetectionNumberApi;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.CarInfo;
import com.czy.owner.global.Constants;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.PhoneUtils;

import java.util.HashMap;

import butterknife.BindView;

/**
 * Created by King on 2017-05-15
 * 行驶里程
 */

public class MaintenanceDetectionActivity extends BaseActivity {
    @BindView(R.id.tv_detection_count)
    TextView tvCount;
    @BindView(R.id.et_maintenance_detection_mileage)
    EditText etMileage;
    @BindView(R.id.btn_maintenance_detection)
    Button btnDetection;

    private CarInfo mCarInfo;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_maintenance_detection;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("行驶里程");
        getSubTitle().setText("保养手册");
        tvCount.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        if (getIntent().getExtras() == null || (mCarInfo = (CarInfo) getIntent().getSerializableExtra(Constants.TRANSMISSION_CONTENT_KEY)) == null) {
            PhoneUtils.ShowToastMessage(this, "参数异常!");
            finish();
            return;
        }
        getMaintainDetectionNumber();
    }

    @Override
    protected void updateViews() {
        etMileage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().length() == 0 ){
                    btnDetection.setEnabled(false);
                }else{
                    btnDetection.setEnabled(true);
                }
            }
        });

        btnDetection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(etMileage.getText().length() > 0 ){
                    startActivityForResult(new Intent(MaintenanceDetectionActivity.this, DetectionResultActivity.class).putExtra(Constants.TRANSMISSION_CONTENT_KEY, mCarInfo).putExtra("mileage",etMileage.getText().toString()), 0);
                }
            }
        });
        getSubTitle().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(MaintenanceDetectionActivity.this, MaintenanceManualActivity.class).putExtra(Constants.TRANSMISSION_CONTENT_KEY, mCarInfo), 0);
            }
        });
    }

    private void getMaintainDetectionNumber(){
        MaintenanceDetectionNumberApi mApi = new MaintenanceDetectionNumberApi(new HttpOnNextListener<Integer>() {
            @Override
            public void onNext(Integer o) {
                if(o > 1000){
                    tvCount.setText("" + o);
                }else{
                    tvCount.setText("1000");
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
            }
        }, this);
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("session", UserHelper.getInstance().getSession(this));
        mApi.setMap(requestMap);
        HttpManager.getInstance().doHttpDeal(mApi);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            finish();
        }
    }
}
